package com.herui.sdyu_lib.utils.ble;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Handler;

/**
 * Created by amanda.shan on 2016/9/20.
 */
public class BleScan implements BluetoothAdapter.LeScanCallback{

    private BluetoothAdapter mBluetoothAdapter;
    private Context context;

    public BleScan(Context context) {

        this.context = context;
        // Initializes Bluetooth adapter.
        final BluetoothManager bluetoothManager =
                (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
        mBluetoothAdapter = bluetoothManager.getAdapter();

        // 确保蓝牙在设备上可以开启
        if (mBluetoothAdapter == null || !mBluetoothAdapter.isEnabled()) {
            mBluetoothAdapter.enable();
        }
    }


    private boolean mScanning;
    private Handler mHandler = new Handler();
    // 10秒后停止寻找.
    private static final long SCAN_PERIOD = 2000;

    private void scanLeDevice(final boolean enable) {
        if (enable) {
            // 经过预定扫描期后停止扫描
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mScanning = false;
                    mBluetoothAdapter.stopLeScan(BleScan.this);
                }
            }, SCAN_PERIOD);
            mScanning = true;
            mBluetoothAdapter.startLeScan(BleScan.this);
        } else {
            mScanning = false;
            mBluetoothAdapter.stopLeScan(BleScan.this);
        }
    }

    @Override
    public void onLeScan(BluetoothDevice device, int rssi, byte[] scanRecord) {

    }
}
